﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing;


namespace SPBusiness
{
   public sealed class Navigation
    {
              /// <summary>
       /// Navigation class has only Static Methods, no constructor needed.
       /// </summary>
       private Navigation() { }

       public static SPNavigationNodeCollection GetGlobalNavigation(string pSiteUrl, string pWebRelativeUrl)
        {
            Guid publishingWebID = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
            SPNavigationNodeCollection vSPNavigationNodeCollection = null;
            try
            {
               //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
   using (SPSite vSPSite = new SPSite(pSiteUrl))
   {
      using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
      {

         if (vSPWeb.Features.OfType<SPFeature>().Any(x => x.DefinitionId == publishingWebID))
         {
            PublishingWeb vPublishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);
            vSPNavigationNodeCollection = vPublishingWeb.Navigation.GlobalNavigationNodes;
         }
         else
         {
            vSPNavigationNodeCollection = vSPWeb.Navigation.QuickLaunch;
         }
      }
   }
//});
        }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "GetGlobalNavigation", ex);
                throw;
            }

            return vSPNavigationNodeCollection;
        }

        public static SPNavigationNodeCollection GetLocalNavigation(string pSiteUrl, string pWebRelativeUrl)
        {
            Guid publishingWebID = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
            SPNavigationNodeCollection vSPNavigationNodeCollection = null;
            try
            {
               //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
   using (SPSite vSPSite = new SPSite(pSiteUrl))
   {
      using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
      {

         if (vSPWeb.Features.OfType<SPFeature>().Any(x => x.DefinitionId == publishingWebID))
         {
            PublishingWeb vPublishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);
            vSPNavigationNodeCollection = vPublishingWeb.Navigation.CurrentNavigationNodes;
         }
         else
         {
            vSPNavigationNodeCollection = vSPWeb.Navigation.QuickLaunch;
         }
      }
   }
//});
        }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "GetLocalNavigation", ex);
                throw;
            }

            return vSPNavigationNodeCollection;
        }

        public static void EmptyNavigation(string pSiteUrl, string pWebRelativeUrl)
        {

            try
            {
               //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
   using (SPSite vSPSite = new SPSite(pSiteUrl))
   {
      using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
      {

         SPNavigationNodeCollection quicklaunchNodes = vSPWeb.Navigation.QuickLaunch;
         for (int i = quicklaunchNodes.Count - 1; i >= 0; i--)
         {
            quicklaunchNodes.Delete(quicklaunchNodes[i]);
         }
         SPNavigationNodeCollection topNavigationNodes = vSPWeb.Navigation.TopNavigationBar;
         for (int i = topNavigationNodes.Count - 1; i >= 0; i--)
         {
            topNavigationNodes.Delete(topNavigationNodes[i]);
         }

         PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);

         SPNavigationNodeCollection globalNavigationNodes = publishingWeb.Navigation.GlobalNavigationNodes;

         for (int i = globalNavigationNodes.Count - 1; i >= 0; i--)
         {
            globalNavigationNodes.Delete(globalNavigationNodes[i]);
         }

         SPNavigationNodeCollection currentNavigationNodes = publishingWeb.Navigation.CurrentNavigationNodes;
         for (int i = currentNavigationNodes.Count - 1; i >= 0; i--)
         {
            currentNavigationNodes.Delete(currentNavigationNodes[i]);
         }

         publishingWeb.Navigation.AutomaticSortingMethod = AutomaticSortingMethod.CreatedDate;

         if (!vSPWeb.IsRootWeb)
         {
            globalNavigationNodes.Navigation.UseShared = true;
         }
         publishingWeb.Navigation.CurrentIncludePages = false;
         publishingWeb.Navigation.CurrentIncludeSubSites = true;
         publishingWeb.Navigation.GlobalIncludePages = false;
         publishingWeb.Navigation.GlobalIncludeSubSites = true;

      }
   }
//});
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "EmptyNavigation", ex);
                throw;
            }
        }

        public static void EmptyCurrentNavigation(string pSiteUrl, string pWebRelativeUrl)
        {

            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
        {

            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);

            SPNavigationNodeCollection currentNavigationNodes = publishingWeb.Navigation.CurrentNavigationNodes;
            for (int i = currentNavigationNodes.Count - 1; i >= 0; i--)
            {
                if ((int) currentNavigationNodes[i].TargetParentObjectType != -1)
                {
                    currentNavigationNodes.Delete(currentNavigationNodes[i]);
                }
            }
        }
    }
//});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "EmptyCurrentNavigation", ex);
                throw;
            }
        }

        public static void CreateSubNavigation(string pSiteUrl, string pWebRelativeUrl)
        {
            try
            {
               //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
   using (SPSite vSPSite = new SPSite(pSiteUrl))
   {
      using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
      {

         SPNavigationNodeCollection quicklaunchNodes = vSPWeb.Navigation.QuickLaunch;
         for (int i = quicklaunchNodes.Count - 1; i >= 0; i--)
         {
            quicklaunchNodes.Delete(quicklaunchNodes[i]);
         }
         SPNavigationNodeCollection topNavigationNodes = vSPWeb.Navigation.TopNavigationBar;
         for (int i = topNavigationNodes.Count - 1; i >= 0; i--)
         {
            topNavigationNodes.Delete(topNavigationNodes[i]);
         }
         PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);
         SPNavigationNodeCollection globalNavigationNodes = publishingWeb.Navigation.GlobalNavigationNodes;
         for (int i = globalNavigationNodes.Count - 1; i >= 0; i--)
         {
            globalNavigationNodes.Delete(globalNavigationNodes[i]);
         }

         SPNavigationNodeCollection currentNavigationNodes = publishingWeb.Navigation.CurrentNavigationNodes;
         for (int i = currentNavigationNodes.Count - 1; i >= 0; i--)
         {
            currentNavigationNodes.Delete(currentNavigationNodes[i]);
         }

         globalNavigationNodes.Navigation.UseShared = true;
         publishingWeb.Navigation.CurrentIncludePages = true;
         publishingWeb.Navigation.GlobalIncludePages = false;

      }
   }
//});


            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "CreateSubNavigation", ex);
                throw;
            }
        }

        public static void ConfigGlobalNavigation(string pSiteUrl, string pWebRelativeUrl, bool pInheritGlobal, int pGlobalDynamicChildLimit, bool pGlobalIncludePages, bool pGlobalIncludeSubSites)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
        {
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);

            publishingWeb.Navigation.InheritGlobal = pInheritGlobal;
            publishingWeb.Navigation.GlobalDynamicChildLimit = pGlobalDynamicChildLimit;
            publishingWeb.Navigation.GlobalIncludePages = pGlobalIncludePages;
            publishingWeb.Navigation.GlobalIncludeSubSites = pGlobalIncludeSubSites;

            publishingWeb.Update();
            publishingWeb.Web.Update();
        }
    }
//});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "ConfigGlobalNavigation", ex);
                throw;
            }
        }

        public static void ConfigCurrentNavigation(string pSiteUrl, string pWebRelativeUrl, bool pInheritCurrent, int pCurrentDynamicChildLimit, bool pCurrentIncludePages, bool pCurrentIncludeSubSites, bool pShowSiblings)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
//{
    using (SPSite vSPSite = new SPSite(pSiteUrl))
    {
        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
        {
            PublishingWeb vPublishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);

            vPublishingWeb.Navigation.InheritCurrent = pInheritCurrent;
            vPublishingWeb.Navigation.ShowSiblings = pShowSiblings;
            vPublishingWeb.Navigation.CurrentDynamicChildLimit = pCurrentDynamicChildLimit;
            vPublishingWeb.Navigation.CurrentIncludePages = pCurrentIncludePages;
            vPublishingWeb.Navigation.CurrentIncludeSubSites = pCurrentIncludeSubSites;

            vPublishingWeb.Update();
            vPublishingWeb.Web.Update();
        }
    }
//});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "ConfigCurrentNavigation", ex);
                throw;
            }
        }

        public static void AddToTopNavigation(string pSiteUrl, string pWebRelativeUrl, string pTitle, string pUrl, bool pIsExternal)
        {
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebRelativeUrl))
                        {

                            PublishingWeb vPublishingWeb = PublishingWeb.GetPublishingWeb(vSPWeb);

                            SPNavigationNodeCollection vSPNavigationNodeCollection = vPublishingWeb.Navigation.GlobalNavigationNodes;

                            if (!vSPNavigationNodeCollection.OfType<SPNavigationNode>().Any(x => x.Title == pTitle))
                            {

                                SPNavigationNode vSPNavigationNode = new SPNavigationNode(pTitle, pUrl, pIsExternal);
                                vSPNavigationNodeCollection.AddAsLast(vSPNavigationNode);
                                vSPNavigationNode.Update();
                                vPublishingWeb.Update();
                            }

                        }
                    }
                //});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(Navigation).Namespace, typeof(Navigation).Name, "AddToTopNavigation", ex);
                throw;
            }
        }
    }
}
